/*===============================================================
 *   Copyright (C) 2020 All rights reserved.
 *   
 *   文件名称：car.c
 *   创 建 者：liuaoyu
 *   创建日期：2020年09月03日
 *   描    述：
 *
 *   更新日志：
 *
 ================================================================*/
#include "../include/server.h"

void exectue(sqlite3 *mysql,char *sql);
char *sql;
char *zErrMsg = 0;//保存出错信息


char sql_buff[50] = {0};    //存储sql语句
int sql_ret;        //接收返回值
//回调函数
static int callback(void *NotUsed,int argc,char **argv,char **azColName)
{
    int i,fd,nbytes;
    if((fd = open("data.txt",O_CREAT | O_RDWR)) < 0)
    {
        puts("open error.");
    }
    for(i = 0;i < argc;i++)
    {
        printf("%s = %s\n",azColName[i],argv[i] ? argv[i] : "NULL");
        nbytes = write(fd,argv[i],100) ;
    }
    close(fd);
    printf("\n");
    return 0;
}
/*定义创建一个数据库*/
sqlite3 *mysql_create()
{
    sqlite3 *db;
    int rc;

    rc = sqlite3_open("car.db",&db);
    if(NULL == db)
    {
        puts("sqlite3_open error.");
        return NULL;
    }
    puts("sqlite3_open success.");
    return db;
}

/*创建一个表*/
char *sql_table_create()
{
    sql = "create table car"    \
           "(id int primary key not null,"   \
           "status text not null,"  \
           "lock text not null);";
    return sql;
}
/*插数插入*/
char *sql_table_insert()
{
    sql = "insert into car(id,status,lock)" \
           "values(1,'损坏','未开锁');"    \
           "insert into car(id,status,lock)"\
           "values(2,'正常','未开锁');"    \
           "insert into car(id,status,lock)"\
           "values(3,'正常','已开锁');";    \

           return sql;
}

sqlite3 *sql_init(sqlite3 *mysql)
{
    mysql = mysql_create();
    sql = sql_table_create();
    exectue(mysql,sql);
    sql = sql_table_insert();
    exectue(mysql,sql);
    return mysql;
}
/*判断数据和数据库的关系*/
int find_data()
{
    int fd;
    fd = open("data.txt",O_RDONLY);
    char buf[100];
    char txt[100];
    while(read(fd,buf,100))
    {
        strcat(txt,buf);
    }
    if(NULL != strstr(txt,"正常"))
    {
        if(NULL != strstr(txt,"已开锁"))
        {
            return 1;
        }
        else
        {
            return 2;
        }
    }
    else
    {
        return 3;
    }
    close(fd);
}
/*数据查找*/
int sql_table_find(sqlite3 *mysql,char *buf)//buf保存的是用户发送的信息,比如id
{
    sprintf(sql_buff,"select * from car where id=%s",buf);
    exectue(mysql,sql_buff);
    sql_ret = find_data();
    return sql_ret;
}
/*数据更新*/
int sql_table_update(sqlite3 *mysql,char *buf)
{
    sprintf(sql_buff,"update car set lock='已开锁' where id = %s",buf);
    exectue(mysql,sql_buff);
    sql_ret = sql_table_find(mysql,buf);
    sql_ret = find_data();
    return sql_ret;
}
/*执行sql语句*/
void exectue(sqlite3 *mysql,char *sql)
{
    int rc;
    rc = sqlite3_exec(mysql,sql,callback,0,&zErrMsg);
    if(rc != SQLITE_OK)//执行成功返回SQLITE_OK
    {
        fprintf(stderr,"SQL err:%s\n",zErrMsg);//打印错误信息
    }
    else
    {
        fprintf(stdout,"table created successfully.\n");
    }
}


/*获取当前时间*/
void NowTime()
{
    time_t t;
    struct tm *p;
    time(&t);
    p = localtime(&t);//获取当前时间
    printf("%d-%d-%d %d:%d:%d\n",1900+p->tm_year,1+p->tm_mon,p->tm_mday,\
            p->tm_hour,p->tm_min,p->tm_sec);
}

